package zhuyouyong.agp8

import com.android.build.api.instrumentation.AsmClassVisitorFactory
import com.android.build.api.instrumentation.ClassContext
import com.android.build.api.instrumentation.ClassData
import com.android.build.api.instrumentation.InstrumentationParameters
import org.objectweb.asm.ClassVisitor
import zhuyouyong.ObfuscateExtension
import zhuyouyong.bytecode.StringFieldClassVisitor

/**
 * Created by zhuyouyong@bm-intelligent.com on 2023/11/27.
 */
abstract class ObfuscateTransform : AsmClassVisitorFactory<InstrumentationParameters.None> {
    companion object {
        lateinit var obfuscateExtension: ObfuscateExtension
    }

    override fun createClassVisitor(
        classContext: ClassContext,
        nextClassVisitor: ClassVisitor
    ): ClassVisitor {
        return StringFieldClassVisitor(nextClassVisitor)
    }

    override fun isInstrumentable(classData: ClassData): Boolean {
        if (obfuscateExtension.enabled) {
            obfuscateExtension.packages.forEach {
                if (classData.className.startsWith(it)) {
                    return true
                }
            }
        }
        return false
    }
}